package model;

import java.util.Collection;
import data.Project;

public class EffortEstimation implements Calculator {

//	public int similarityfunction(Project p1, Project p2){
//		return 0;
//	}
	
	//R�kna ut effort genom att ta similarity listan * effort / antal similarity f�r att f� fram medeltalet p� effort.
	@Override
	public double calculate( Project p , Collection<Project> projectlist, double threshold){ // Skickar in en typ av databas ? 
		
		SimilarityFunction similar = new SimilarityFunction();
		similar.initMinMax(projectlist);

		double max1 = -1;
		double max2 = -1;
		Project[] proj = new Project[2];
		// hämtar ut alla projekt från projectlist som är lik Project p. Detta görs genom att skicka in seperat p och varje element i projektlist 
		for (Project e : projectlist ) {
			double similarity  = similar.similarityFunction(p, e);
			
			if (similarity > max1 || similarity > max2) {
				if (max1 > max2) {
					max2 = similarity;
					proj[1] = e;
				} else {
					max1 = similarity;
					proj[0] = e;
				}
			}	
				
		}
		
		if (max1 < threshold || max2 < threshold){
			return -1;
		} else{									
		
//			System.out.println("test: " + proj[1]);
//			System.out.println("test: " + proj[1].params);
//			System.out.println("test: " + proj[1].params.get("Project Size"));
			double psize = p.params.get("Project Size").dvalue;
			double psize1 = proj[0].params.get("Project Size").dvalue;
			double psize2 = proj[1].params.get("Project Size").dvalue;
			double effort1 = proj[0].params.get("Actual effort(months)").dvalue;
			double effort2 = proj[1].params.get("Actual effort(months)").dvalue;
			
			
			double effortestimation= ( psize/ psize1 ) * effort1 * (max1/(max1+max2)) + 
					( psize/ psize2 ) * effort2 * (max2/(max1+max2)); 

			boolean dbprint=false;
			if(dbprint) {
				System.out.println("Project 0: "+ p);
				System.out.println("Project 1: "+ proj[0]);
				System.out.println("Proejct 2: "+ proj[1]);
				
				System.out.println("Project 1 effort, size: "+ effort1 +", "+ psize1);
				System.out.println("Project 1 similarity: "+ max1);
	
				System.out.println("Project 2 effort, size: "+ effort2 +", "+ psize2);
				System.out.println("Project 2 similarity: "+ max2);
			}
			return effortestimation;
		}
	}

	public void dbug(String s) {
		System.out.println(s);
	}
}
